package Thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockTest {
    public Lock lock = new ReentrantLock();

    public Condition condition1 = lock.newCondition();

    public Condition condition2 = lock.newCondition();

    int num = 10;

    public void test() {
        lock.lock();
        System.out.println(Thread.currentThread().getName()+"lock");
//        try {
//            lock.lockInterruptibly();
//        } finally {
//            lock.unlock();
//        }
    }

    public void test2(){
        try {
            lock.lockInterruptibly();
            System.out.println(Thread.currentThread().getName()+"lock acquired in test2()");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        LockTest lockTest = new LockTest();
        new Thread(() -> {
            lockTest.test();
        }).start();

        new Thread(() -> {
            lockTest.test2();
        }).start();
//        new Thread(() -> {
//            lockTest.test();
//        }).start();
    }

}
